using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace SpiderService
{
	public abstract class ThreadBase
	{
		private Thread thread;
		protected bool started;

		public bool Started
		{
			get { return this.started; }
		}

		public void Start()
		{
			if (this.started)
				return;
			this.started = true;
			this.thread = new Thread(new ThreadStart(this.DoWork));
			this.thread.IsBackground = true;
			this.thread.Start();
		}

		protected virtual void OnStop()
		{
		}

		public void Stop()
		{
			this.started = false;
			this.OnStop();
			if (thread != null)
			{
				if (!thread.Join(TimeSpan.FromSeconds(3)))
				{
					try
					{
						thread.Abort();
					}
					catch
					{
					}
				}
			}
		}

		public bool IsAlive
		{
			get
			{
				return (thread != null && thread.IsAlive);
			}
		}

		protected abstract void DoWork();
	}
}
